package com.w7vm.menuweb.managerBean;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpSession;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;

import com.w7vm.menuweb.entity.AcessoWeb;
import com.w7vm.menuweb.entity.UsuarioWeb;
import com.w7vm.menuweb.facade.AcessoFacade;
import com.w7vm.menuweb.facade.UserFacade;

@ManagedBean
@SessionScoped
public class LoginMB{

	private AcessoWeb  acesso;
	private UsuarioWeb usuario;
	
	@EJB  
	private AcessoFacade acessoEJB;
		
	private Object sessao;
	
	private List<AcessoWeb> acessoL = null;
	private String usuarioSystem = null;

	FacesContext context = FacesContext.getCurrentInstance();
    HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
	
	@PostConstruct
	public void loadSession() {
    	System.out.println("[Method][loadSession]");
	    acesso = getAcessoList();
	    session.setAttribute("IDUsuario", acesso.getUsuarioWeb().getIdUsuarioWeb());
	    session.setAttribute("Usuario", acesso.getUsuarioWeb().getUsername());
	    session.setAttribute("NomeUsuario", acesso.getUsuarioWeb().getNome());
	    session.setAttribute("IDRestaurante", null);
	    session.setAttribute("AcessoFilial", null);
	    session.setAttribute("NomeFantasia", null);
	    session.setAttribute("IDSede", null);
	    session.setAttribute("cnpj", acesso.getRestauranteSedeWeb().getRestauranteSedeJuridicoWeb().getCnpj());
	    session.setAttribute("Perfil", acesso.getPerfil());
	    session.setAttribute("DtTerminoContrato", "31/12/2014");
	}
	
	public void gravarSessaoPerfilUsuario(int dados){
	    session.setAttribute("IDPerfilUsuario", dados);
	}

	public void gravarSessao(String name, Object dado){
	    session.setAttribute(name, dado);
	}

	public AcessoWeb getAcessoList(){
    	System.out.println("Usuario Na Sessão [getAcessoList()]: "+ usuarioSystem);
	    acessoL = acessoEJB.findAcessoRestauranteByUsuario(usuarioSystem);
    	System.out.println("[getAcessoList()] List: "+ acessoL);
    	System.out.println("[getAcessoList()] List: "+ acessoL.size());

	    AcessoWeb acessoObj = null;
		for (int i = 0; i < acessoL.size(); i++) {
			acessoObj = acessoL.get(i);
		}
		return acessoObj;
	}

	public Object getSessao(String opcao) {
		System.out.println("[Method][getSessao]");
	    FacesContext context = FacesContext.getCurrentInstance();
	    HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
	    return session.getAttribute(opcao);
	}

	public void setSessao(String sessao) {
		this.sessao = sessao;
	}

		
	public LoginMB() {
		
		System.out.println("LoginMB");
		usuario = new UsuarioWeb();
	    SecurityContext context = SecurityContextHolder.getContext();

	    if (context instanceof SecurityContext){
	    	System.out.println("authentication");
	        Authentication authentication = context.getAuthentication();
	        if (authentication instanceof Authentication){
	        	usuarioSystem = ((User)authentication.getPrincipal()).getUsername();
	        	usuario.setUsername(((User)authentication.getPrincipal()).getUsername());
	        }
	    }
	}
	    
	private void invalidateSession() {
		FacesContext fc = FacesContext.getCurrentInstance();
	    HttpSession session = (HttpSession) fc.getExternalContext().getSession(false);
	    session.invalidate();
	}
	 
	public UsuarioWeb getUsuario() {
	    return usuario;
	}
	 
	public void setUsuario(UsuarioWeb usuario) {
		this.usuario = usuario;
	}
	   
}
